package com.jaya.budan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.jaya.budan.R;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class Privacy_WebView_Activity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout back;
    private String access_token;
    private int type;
    private WebView web_zhuce;
    private TextView tv_privacy_name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_privacy);
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        type = intent.getIntExtra("type", 0);
        back = findViewById(R.id.back);
        tv_privacy_name = findViewById(R.id.tv_privacy_name);
        web_zhuce = findViewById(R.id.web_zhuce);
        back.setOnClickListener(this);
        if (type == 1){
            tv_privacy_name.setText("不单用户条款");
        } else if (type == 2){
            tv_privacy_name.setText("不单行为规范");
        } else if (type == 3){
            tv_privacy_name.setText("隐私条款");
        } else if (type == 4){
            tv_privacy_name.setText("个人推广使用规范");
        } else if (type == 5){
            tv_privacy_name.setText("个人推广用户协议");
        } else if (type == 6){
            tv_privacy_name.setText("广场推广使用规范");
        } else if (type == 7){
            tv_privacy_name.setText("广场推广服务协议");
        } else if (type == 8){
            tv_privacy_name.setText("布丁充值服务协议");
        } else if (type == 9){
            tv_privacy_name.setText("不单会员服务条款");
        } else if (type == 10){
            tv_privacy_name.setText("应用评价");
            web_zhuce.loadUrl("http://a.app.qq.com/o/simple.jsp?pkgname=cn.handanlutong.parking");//加载本地html
        }
        postData();
//        web_zhuce.loadUrl("file:///android_asset/agreement_cz.html");//加载本地html
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData() {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.privacy_web_Url + "&type=" + type, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("privacy", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONObject info = jsonObject.getJSONObject("info");
                        String content = info.getString("content");
                        web_zhuce.loadUrl(content);//加载html内容
                        WebSettings settings = web_zhuce.getSettings();
                        settings.setJavaScriptEnabled(true);//启用支持javascript
                        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//使用缓存
                        web_zhuce.setWebViewClient(new WebViewClient(){
                            @Override
                            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                                view.loadUrl(url);
                                return true;
                            }
                        });
                        settings.setSupportZoom(true);// 设置WebView可点击缩放
                        settings.setBuiltInZoomControls(true);// 设置WebView可触摸放大缩小
                        settings.setUseWideViewPort(true);
                        settings.setLoadWithOverviewMode(true);//自动适应屏幕
                    } else {
                        ToolUtils.showToast(Privacy_WebView_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(Privacy_WebView_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }
}
